import type { ItemSchema } from '@/typing/helper';
import { StudentField } from '@/components';
import { getDictTextByCode } from '@/data/dict';

import {
  textField,
  switchField,
  dividerField,
  dictField,
  selectField,
  entityField,
} from '@/helpers';

export const baseFormSchema: ItemSchema[] = [
  dividerField('普通文本'),
  textField('name', '姓名', { required: true }),
  dividerField('文本域'),
  textField('address', '地址', { type: 'textarea' }),
  dividerField('是否字段'),
  switchField('isMarried', '是否婚配'),
  dividerField('字典选择'),
  dictField('marital', '婚姻情况', 'marital'),
  dividerField('select'),
  selectField('way', '交通工具', [
    { text: '汽车', code: 1 },
    { text: '步行', code: 2 },
  ]),
  dividerField('实体选择(学生)'),
  entityField('studentId', '学生', StudentField, {
    change(record) {
      console.log(record);
    },
  }),
];

export const showFormSchema: ItemSchema[] = [
  switchField('isPoor', '是否是家困生', {
    change(value, _, formSchema) {
      console.log(value);
      if (value) {
        formSchema![1]!.props!.show = undefined;
      } else {
        formSchema![1]!.props!.show = false;
      }
    },
  }),
  entityField('poorStudent', '家困生信息', StudentField, {
    show: false,
  }),
];

export const entityFormSchema: ItemSchema[] = [
  entityField('poorStudent', '家困生信息', StudentField, {
    change(record, formValue) {
      console.log(record, formValue);
      const { name, sex, age, studentClass } = record;
      formValue['name'] = name;
      formValue['sex'] = getDictTextByCode('sex', sex);
      formValue['age'] = age;
      formValue['studentClass'] = studentClass;
    },
  }),
  textField('name', '姓名', { disabled: true }),
  textField('sex', '性别', { disabled: true }),
  textField('age', '年龄', { disabled: true }),
  textField('studentClass', '班级', { disabled: true }),
];
